#include "testrunner.h"


bool TestRunner::RunTests(const CPath& testDll)
{
    ::SetErrorMode(SEM_FAILCRITICALERRORS);   // tell the OS to not pop-up message boxes for anything other than critical errors

    class ResetCurrentDirectory
    {
        CString m_orig;
    public:
        ResetCurrentDirectory(const CString& testDll)
        {
            ::GetCurrentDirectory(MAX_PATH, m_orig.GetBuffer(MAX_PATH));
            m_orig.ReleaseBuffer();

            CPath path(testDll);
            path.RemoveFileSpec();
            ::SetCurrentDirectory(path);
        }
        ~ResetCurrentDirectory()
        {
            ::SetCurrentDirectory(m_orig);
        }
    } rcd(testDll);

    bool b = false;
    HINSTANCE hinst = LoadLibrary(testDll);
    if (hinst) 
    {
        void (__stdcall *fnRunTddTests)(TDD::Discriminator&, TDD::Reporter&);
        fnRunTddTests = (void (__stdcall*)(TDD::Discriminator&, TDD::Reporter&))GetProcAddress(hinst, "RunTddTests");
        if (fnRunTddTests) {
            b = true;
            ::CoInitialize(NULL); // for test writers' convenience only
            fnRunTddTests(m_discriminator, m_reporter);
            ::CoUninitialize();
        }
        ::FreeLibrary(hinst);
    }
    return b;
}
